package com.app.shop.service.system.impl;

import com.app.shop.util.SysModel;
import com.app.shop.dao.system.SysMenuMapper;
import com.app.shop.dao.system.SysReAuthMenuMapper;
import com.app.shop.dao.system.SysReAuthUserMapper;
import com.app.shop.entity.SysMenu;
import com.app.shop.entity.SysMenuExample;
import com.app.shop.entity.query.SysMenuQuery;
import com.app.shop.service.system.SysMenuService;
import com.app.shop.entity.show.SysMenuShow;
import com.app.shop.util.ClassUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 系统菜单
 *
 * @author chenliwei
 * @version v1.0 2019-3-11
 */
@Service("sysMenuService")
public class SysMenuServiceImp implements SysMenuService {

    private static final Logger log = LoggerFactory.getLogger(SysMenuServiceImp.class);

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysReAuthUserMapper sysReAuthUserMapper;
    @Autowired
    private SysReAuthMenuMapper sysReAuthMenuMapper;

    @Override
    public List<SysMenu> getSysMenuList(SysMenuQuery sysMenuQuery) {
        //查询条件
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andStatusEqualTo(1);
        //菜单名称
        if (sysMenuQuery.getMenuName() != null && sysMenuQuery.getMenuName().trim().length() > 0) {
            criteria.andMenuNameLike('%' + sysMenuQuery.getMenuName() + '%');
        }
        //菜单状态
        if (sysMenuQuery.getStatus() != null) {
            criteria.andStatusEqualTo(sysMenuQuery.getStatus());
        }
        //菜单URL
        if (sysMenuQuery.getMenuUrl() != null && sysMenuQuery.getMenuUrl().trim().length() > 0) {
            criteria.andMenuUrlLike('%' + sysMenuQuery.getMenuUrl() + '%');
        }
        return sysMenuMapper.selectByExample(sysMenuExample);
    }

    @Override
    public SysMenuShow getMenuInfo(SysMenuQuery sysMenuQuery) {
        SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(sysMenuQuery.getMenuId());
        SysMenuShow sysMenuShow = new SysMenuShow();
        try {
            ClassUtil.fatherToChild(sysMenu, sysMenuShow);
            //码表值补全
            sysMenuShow.dataSet();
        } catch (Exception e) {
            log.error("菜单信息补全码表过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysMenuShow;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel addMenuInfo(SysMenu sysMenu) {
        SysModel sysModel = new SysModel();
        try {
            //查询用户是否存在
            SysMenuExample sysMenuExample = new SysMenuExample();
            SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
            criteria.andMenuNameEqualTo(sysMenu.getMenuName()).andStatusEqualTo(1);
            sysMenu.setCreateDate(new Date());
            sysMenu.setVersion(new Date());
            //不允许重复菜单
            Long cnt = sysMenuMapper.countByExample(sysMenuExample);
            if (cnt < 1) {
                sysMenuMapper.insert(sysMenu);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("添加失败，菜单已经存在！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("添加失败，添加菜单过程发生异常请重试！");
            log.error("菜单添加过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel updateMenuInfo(SysMenu sysMenu) {
        SysModel sysModel = new SysModel();
        try {
            // 修改要和之前的版本时间对应，不然不允许修改
            SysMenuExample sysMenuExample = new SysMenuExample();
            SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
            criteria.andMenuIdEqualTo(sysMenu.getMenuId()).andVersionEqualTo(sysMenu.getVersion());
            SysMenu sysMenuUp = sysMenuMapper.selectByPrimaryKey(sysMenu.getMenuId());
            if (sysMenuUp != null) {
                sysMenuUp.setStatus(sysMenu.getStatus());
                sysMenuUp.setMenuName(sysMenu.getMenuName());
                sysMenuUp.setMenuUrl(sysMenu.getMenuUrl());
                sysMenuUp.setRemark(sysMenu.getRemark());
                sysMenuUp.setIcon(sysMenu.getIcon());
                sysMenuUp.setDispOrder(sysMenu.getDispOrder());
                sysMenuUp.setVersion(new Date());
                sysMenuUp.setUpdateUserId(sysMenuUp.getUpdateUserId());
                sysMenuMapper.updateByExample(sysMenuUp, sysMenuExample);
                sysModel.setSuccess(true);
            } else {
                sysModel.setSuccess(false);
                sysModel.setMsg("菜单修改失败，当前信息已经被他人修改请重试！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("菜单修改失败，修改过程发生异常请重试！");
            log.error("菜单修改过程中发生异常:" + e);
            e.printStackTrace();
        }
        return sysModel;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateMenuStop(SysMenuQuery sysMenuQuery) {

        SysMenu sysMenu = new SysMenu();
        sysMenu.setStatus(-1);
        sysMenu.setVersion(new Date());
        sysMenu.setMenuId(sysMenuQuery.getMenuId());
        sysMenu.setUpdateUserId(sysMenuQuery.getOptionUserId());
        sysMenuMapper.updateStopByPrimaryKey(sysMenu);

    }




}
